<template>
  <div class="SelectSchool">
    <h5 class="subtitle">第二步</h5>
    <cube-form :model="model" @validate="validateHandler" @submit="submitHandler">
      <cube-form-group>
        <cube-form-item :field="fields[0]">
          <div class="selectedVal" @click="showSchoolPicker">
            <span v-if="model.schoolValue==''" class="placeholder">请选择所在学校</span>
            <span v-if="model.schoolValue!=''">{{model.schoolValue}}</span>
          </div>
        </cube-form-item>
      </cube-form-group>
      <cube-form-group class="subBtn">
        <cube-button @click="goback" :inline="true" :primary="true">上一步</cube-button>
        <cube-button type="submit" :inline="true" :primary="true">下一步</cube-button>
      </cube-form-group>
    </cube-form>
  </div>
</template>

<script>
export default {
  name: "SelectSchool",
  data() {
    return {
      validity: {},
      valid: undefined,
      model: {
        schoolId: "",
        schoolValue: ""
      },
      fields: [
        {
          modelKey: "schoolValue",
          label: "选择学校",
          props: {
            placeholder: "请选择学校"
          },
          rules: {
            required: true
          },
          messages: {
            required: "请选择所在学校"
          },
          events: {}
        }
      ]
    };
  },
  
  mounted() {
    this.setDataPicker = this.$createCascadePicker({
      title: "选择所在学校",
      data: this.$store.state.schools,
      alias: {
        value: "id",
        text: "name"
      },
      onSelect: this.selectHandle,
      onCancel: this.cancelHandle
    });
  },
   
  methods: {
    showSchoolPicker() {
      this.setDataPicker.show();
    },
    selectHandle(selectedVal, selectedIndex, selectedText) {
      this.model.schoolValue = selectedText[0];
      this.model.schoolId = selectedVal[0];
    },
    cancelHandle() {
      this.$createToast({
        type: "correct",
        txt: "取消选择",
        time: 1000
      }).show();
      this.goback();
    },

    validateHandler(result) {
      this.validity = result.validity;
      this.valid = result.valid;
    },
    submitHandler(e) {
      e.preventDefault();
      this.$store.state.schoolId = this.model.schoolId;
      localStorage.setItem('schoolId',this.model.schoolId)
      // console.log(localStorage.getItem('schoolId'))
      if (this.$route.path.split("/")[1] == "teaadd") {
        this.$router.push({
          path: "/teaadd/account",
          query: {
            province: this.$route.query.province,
            region: this.$route.query.region,
            schoolId: this.model.schoolId
          }
        });
      } else {
        this.$router.push({
          path: "/paradd/year",
          query: {
            province: this.$route.query.province,
            region: this.$route.query.region,
            schoolId: this.model.schoolId
          }
        });
      }
    },
    goback() {
      this.$router.push({
        path: "/" + this.$route.path.split("/")[1],
        query: {
          province: this.$route.query.province,
          region: this.$route.query.region
        }
      });
    }
  }
};
</script>

<style lang="scss">
.SelectSchool {
  .subtitle {
    font-size: 16px;
    font-weight: 700;
    color: #fc9153;
    margin-top: 20%;
    margin-bottom: 10%;
    text-align: center;
  }
  /* //设置存储picker选择获取的值的div的样式 */
  .selectedVal {
    text-align: left;
    /* 设置存储picker选择获取的值的div显示placeholder时字体的颜色 */
    .placeholder {
      color: #ccc;
    }
  }
  .subBtn {
    margin: 5% auto;
    text-align: center;
    margin-top: 20%;
    button {
      width: 40%;
      margin: 2%;
    }
  }
}
</style>